package com.chanceit.ssss.dialog;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import antistatic.spinnerwheel.AbstractWheel;
import antistatic.spinnerwheel.adapters.NumericWheelAdapter;

import com.chanceit.ssss.activity.R;

/**
 * a wheel date piecker dialog base android style
 * 
 * @author geliang
 * 
 */
public class DataPickWheelDialog extends Dialog {
	private static final int _1990 = 1990;
	/**
	 * @deprecated this style have 31 days at February and any other months.D:
	 */
	public final static int StyleDayPick = 3;
	public final static int StyleMonthPick = 2;
	public final static int StyleYearPick = 1;
	public final static int StyleDatePick = 0;
	private int defaultStyle = StyleDatePick;
	protected String lastDate;

	public DataPickWheelDialog(Context context, int style) {
		super(context);
		defaultStyle = style;
		setCanceledOnTouchOutside(true);
		setTitle("Date Picker");
		LayoutInflater mLayoutInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		view = mLayoutInflater.inflate(R.layout.dialog_wheel_data_picker, null);
		one = (AbstractWheel) view.findViewById(R.id.one);
		two = (AbstractWheel) view.findViewById(R.id.two);
		three = (AbstractWheel) view.findViewById(R.id.three);
		one.setVisibleItems(5);
		one.setViewAdapter(new NumericWheelAdapter(context, _1990, 2036));
		one.setCyclic(true);

		two.setVisibleItems(5);
		two.setViewAdapter(new NumericWheelAdapter(context, 1, 12, "%02d"));
		two.setCyclic(true);

		three.setVisibleItems(5);
		three.setViewAdapter(new NumericWheelAdapter(context, 1, 31, "%02d"));
		three.setCyclic(true);
		switch (defaultStyle) {
		case StyleYearPick:
			two.setVisibility(View.GONE);
			three.setVisibility(View.GONE);
			break;
		case StyleMonthPick:
			three.setVisibility(View.GONE);

			break;
		case StyleDayPick:

			break;
		default:
		case StyleDatePick:

			break;
		}
		setContentView(view);
		setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				String current = getCurrentYearMonth();
				System.out.println("onDismiss:"+current);
				if (DateChangeListener!=null) {
					
					if (!current.equals(lastDate)) {
						DateChangeListener.dateChange(lastDate, current);
					}
				}
			}
		});
		setOnShowListener(new OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				lastDate = getCurrentYearMonth();
				
			}
		});
		setCurrentDate(Calendar.getInstance().get(Calendar.YEAR), Calendar
				.getInstance().get(Calendar.MONTH));
		
		 Window window = this.getWindow();
		 window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
//		 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		  //设置显示动画
		  window.setWindowAnimations(R.style.dialogWindowAnim);
		  WindowManager.LayoutParams wl = window.getAttributes();
		  wl.x = 0;
		  wl.y = 480;
		  wl.alpha = 0.95f;
		  //设置显示位置
		  onWindowAttributesChanged(wl);
	}

	View view;
	AbstractWheel one;
	AbstractWheel two;
	AbstractWheel three;
	DateChangeListener DateChangeListener;

	public DataPickWheelDialog(Context context) {
		this(context, 0);
	}

	public void setCurrentDate(int year, int month) {
		int yeadIndex = year - _1990 >= 0 ? year - _1990 : _1990;
		one.setCurrentItem(yeadIndex, false);
		int monthIndex = (month > 0 && month < 13) ? (month-1) : 1;
		two.setCurrentItem(monthIndex, false);
	};

	public String getCurrentYearMonth() {
		return (one.getCurrentItem() + _1990) + "-"
				+ (String.format("%02d", two.getCurrentItem() + 1));
	}

	public void setDateChangeListener(DateChangeListener mDateChangeListener) {
		this.DateChangeListener = mDateChangeListener;
	}

	public interface DateChangeListener {
		public void dateChange(String previousTime, String currentTime);
	};
}
